﻿using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.DataAccess.UserCode
{
    public partial class MdCustomerDataAccess
    {
        public static DataTable SelectDialogList(string strCustomerID, string strCustomerName, string UserID, int pageIndex, int pageSize, out int m_count)
        {
            try
            {
                #region SQL Server
                string strcmd = @"SELECT distinct A.*,a.CustomerID AS MatCustomerID, 
                                    a.CustomerName AS MatCustomerName,cd.ContactName,cd.Mobile,p.ParameterName as CustomerFromName
                                    FROM MD_Customer A 
                                    left join (select cd.ContactName,cd.Mobile,cd.Tel,cd.Job,cd.Email,cd.QQ,c.CustomerID,c.CustomerDetailID from ( select c.CustomerID,min(cd.CustomerDetailID) CustomerDetailID from MD_Customer c
                                    left join MD_CustomerDetail cd on cd.CustomerID=c.CustomerID
                                    group by c.CustomerID) c
                                    left join MD_CustomerDetail cd on cd.CustomerDetailID=c.CustomerDetailID) cd on a.CustomerID=cd.CustomerID 
                                    left join SYS_Parameter p on p.ParatmeterValue=a.CustomerFrom and p.ParameterTypeID='CustomerFrom'
                                    where 1=1";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(strCustomerID))
                {
                    strcmd += " and A.CustomerID LIKE @CustomerID";
                    dpc.AddWithValue("@CustomerID", "%" + strCustomerID + "%");
                }

                if (!string.IsNullOrEmpty(strCustomerName))
                {
                    strcmd += " and A.CustomerName LIKE @CustomerName";
                    dpc.AddWithValue("@CustomerName", "%" + strCustomerName + "%");
                }
                if (!string.IsNullOrEmpty(UserID))
                {
                    strcmd += " and A.CreateUserId=@UserID";
                    dpc.AddWithValue("@UserID", UserID);
                }

                strcmd += " ORDER BY A.CustomerID DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, pageSize, dpc, out m_count);
                #endregion
            }
            catch
            {
                throw;
            }
        }
    }
}
